package com.pay.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pay.order.dao.PayRateMapper;
import com.pay.order.entity.PayRateSearchDTO;
import com.pay.order.execption.BusinessException;
import com.pay.order.pojo.PayRate;
import com.pay.order.service.PayRateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayRateServiceImpl extends ServiceImpl<PayRateMapper, PayRate> implements PayRateService {
    @Autowired
    private PayRateMapper payRateMapper;

    @Override
    public boolean save(PayRate entity) {
        if(isExit(entity)){
            throw new BusinessException(entity.getName()+"已存在，请勿重复添加");
        }
        payRateMapper.insert(entity);
        return false;
    }

    @Override
    public boolean updateById(PayRate entity) {
        if(isExit(entity)){
            throw new BusinessException(entity.getName()+"已存在，请勿重复添加");
        }
        return payRateMapper.updateById(entity)>0;
    }

    public boolean isExit(PayRate entity){
        QueryWrapper<PayRate> queryWrapper =new QueryWrapper<PayRate>();
        queryWrapper.eq("name",entity.getName());
        queryWrapper.ne("id",entity.getId());
        PayRate payRate = payRateMapper.selectOne(queryWrapper);
        return payRate==null;
    }
    @Override
    public Page<PayRate> queryPageList(PayRateSearchDTO payRate, Integer page, Integer limit) {
        QueryWrapper<PayRate> queryWrapper = new QueryWrapper<PayRate>();
       if(StringUtils.isNotEmpty(payRate.getUname())) {
           queryWrapper.like("name", payRate.getUname());
       }
        return payRateMapper.selectPage(new Page<PayRate>(page,limit),queryWrapper);
    }
}
